package com.cold.legendary.bprocess.trigger.helper;

import com.alibaba.fastjson.JSON;
import com.cold.legendary.bprocess.common.constance.CommonConstances;
import com.cold.legendary.bprocess.context.ins.BPointIns;
import com.cold.legendary.bprocess.context.ins.BProcessIns;
import com.cold.legendary.bprocess.context.manager.BProcessManager;
import com.cold.legendary.bprocess.context.run.BContext;
import com.cold.legendary.bprocess.db.enums.BizStatusEnum;
import com.cold.legendary.bprocess.db.enums.PointRunStatusEnum;
import com.cold.legendary.bprocess.db.model.TbProcessRequest;
import org.apache.commons.lang3.StringUtils;

/**
 * @author cold
 * @since 2023/3/10
 */
public class ProcessRequestHelper {

    public static TbProcessRequest buildInitTbProcessRequest(String bizId, String merchantId, String processId, BContext bContext) {
        BProcessIns processIns = BProcessManager.getBProcessIns(processId);
        BPointIns startBPointIns = processIns.getStartBPointIns();

        String requestId = BProcessManager.getSequence();

        TbProcessRequest initProcessRequest = new TbProcessRequest();
        initProcessRequest.setRequestId(requestId);
        initProcessRequest.setBizId(bizId);
        initProcessRequest.setMerchantId(merchantId);
//        initProcessRequest.setBizScene(bContext.getbiz);
        initProcessRequest.setBizStatus(BizStatusEnum.PROCESSING.getCode());
        initProcessRequest.setPointId(startBPointIns.getPointId());
        initProcessRequest.setPointName(startBPointIns.getPointName());
        initProcessRequest.setProcessId(processId);
        initProcessRequest.setProcessName(processIns.getProcessName());
        initProcessRequest.setBizRefId(bContext.getBizRefId());
        initProcessRequest.setUserId(bContext.getUserId());
        initProcessRequest.setBizTime(bContext.getBizTime());
        initProcessRequest.setProductCode(bContext.getProductCode());
        initProcessRequest.setRemark(CommonConstances.TEXT_START);
        initProcessRequest.setRunContext(JSON.toJSONString(bContext));
        initProcessRequest.setRunContextClazz(bContext.getClass().getName());
        initProcessRequest.setRouteResult(bContext.getRouteResult());
        return initProcessRequest;
    }


    public static TbProcessRequest buildUpdateForContinue(TbProcessRequest qryProcessRequest, BPointIns bPointIns, BContext bContext) {
        TbProcessRequest updateProcessRequest = new TbProcessRequest();
        updateProcessRequest.setRequestId(qryProcessRequest.getRequestId());
        updateProcessRequest.setPointRunStatus(PointRunStatusEnum.PROCESSING.getCode());
        updateProcessRequest.setPointRunMsg("继续执行节点=" + bPointIns.getPointId());
        updateProcessRequest.setRunContext(JSON.toJSONString(bContext));
        updateProcessRequest.setBizRefId(bContext.getBizRefId());
        if(null != bContext.getBizStatusEnum()){
            updateProcessRequest.setBizStatus(bContext.getBizStatusEnum().getCode());
        }
        return updateProcessRequest;
    }


    public static TbProcessRequest buildUpdateTbProcessRequest(BPointIns bPointIns, BContext bContext) {
        //更新流程记录到当前节点
        TbProcessRequest updateProcessRequest = new TbProcessRequest();
        updateProcessRequest.setPointId(bPointIns.getPointId());
        updateProcessRequest.setPointName(bPointIns.getPointName());
        updateProcessRequest.setPointRunMsg("执行节点=" + bPointIns.getPointId());
        updateProcessRequest.setRunContext(JSON.toJSONString(bContext));
        updateProcessRequest.setBizRefId(bContext.getBizRefId());
        String routeResult = bContext.getRouteResult();
        if(StringUtils.isNotBlank(routeResult)){
            updateProcessRequest.setRouteResult(routeResult);
        }
        if(null != bContext.getBizStatusEnum()){
            updateProcessRequest.setBizStatus(bContext.getBizStatusEnum().getCode());
        }
        return updateProcessRequest;
    }
}
